<?php
class CategoryAction extends CommonAction{

	public function index(){
		//     $Category	= M("Category");
		//     $list	=	$Category->limit(5)->order('id desc')->findAll();
		//     $this->assign('list',$list);
		//     $this->display();

		
		//自定义
		$Category=M('Category');

		$condForm['category_no'] = $_POST['category_no'];
		$condForm['category_name'] = $_POST["category_name"];
		$condForm['parent'] = $_POST["parent"];
		$condForm['parent_name'] = $_POST["parent_name"];
		
		if(!empty($condForm['category_no'])){
			$condition['category_no'] = array('like',makeSqlLikeValue($condForm['category_no']));
		}

		if(!empty($condForm['category_name'])){
			$condition['category_name'] = array('like',makeSqlLikeValue($condForm['category_name']));
		}

		if(!empty($condForm['parent'])){
			$condition['parent'] = array('like',makeSqlLikeValue($condForm['parent']));
		}

		if(!empty($condForm['parent_name'])){
			$condition['parent_name'] = array('like',makeSqlLikeValue($condForm['parent_name']));
		}

		import("@.ORG.Page"); //导入分页类
		$count = $Category->count();    //计算总数
		$p = new Page ( $count, 5 );
		if(isset($condition)){
			$list=$Category->where($condition)->limit($p->firstRow.','.$p->listRows)->order('id desc')->findAll();
		}else{
			$list=$Category->limit($p->firstRow.','.$p->listRows)->order('id desc')->findAll();
		}
		$p->setConfig('header','篇记录');
		$p->setConfig('prev',"<");
		$p->setConfig('next','>');
		$p->setConfig('first','<<');
		$p->setConfig('last','>>');
		$page = $p->show ();
		$this->assign("condForm",$condForm);
		$this->assign( "page", $page );
		$this->assign ( "list", $list );
		$this->display();


	}

	public function add(){
		$Category = M("Category");
		$parents = $Category->where("parent=0")->findAll();
		$this->assign("parents", $parents);
		$this-> display();
	}

	public function insert(){
		
// 		dump($_POST);

		$ParentCategory = M("Category");
		$ParentCategory->find($_POST["parent"]);
		$_POST["parent_name"] = $ParentCategory->parent_name;

		if(!empty($_FILES)){
			$uploadFileList = self::_upload($_POST, 'pic');
			$imgPath = $uploadFileList[0]['savename'];
			$_POST["pic"] = $imgPath;
		}
		
		$Category = D("Category");
		if($vo = $Category->create()) {
			$list=$Category->add();
			if($list!==false){
				$this->success('数据保存成功！');
			}else{
				$this->error('数据写入错误！');
			}
		}else{
			$this->error($Form->getError());
		}
	}

	// 更新数据
	public function update() {
		
		$ParentCategory = M("Category");
// 		$ParentCategory->find($_POST["parent"]);
		$_POST["parent_name"] = $ParentCategory->parent_name;
		
		if(!empty($_FILES["pic_u"]["name"])){
			$uploadFileList = self::_upload($_POST, 'pic_u');
			$imgPath = $uploadFileList[0]['savename'];
			$_POST["pic"] = $imgPath;
		}
		
		$Category	=	D("Category");
		if($vo = $Category->create()) {
// 			dump($Category);
			$list=$Category->save();
			if($list!==false){
				$this->success('数据更新成功！');
			}else{
				$this->error("更新失败!");
			}
		}else{
			$this->error($Form->getError());
		}
	}
	// 删除数据
	public function delete() {
		if(!empty($_POST['id'])) {
			$Form	=	M("Category");
			$result	=	$Form->delete($_POST['id']);
			if(false !== $result) {
				$this->ajaxReturn($_POST['id'],'删除成功！',1);
			}else{
				$this->error('删除出错！');
			}
		}else{
			$this->error('删除项不存在！');
		}
	}

	// 编辑数据
	public function edit() {
		if(!empty($_GET['id'])) {
			$Form	=	M("Category");
			$vo	=	$Form->getById($_GET['id']);
			if($vo) {
				$Category = M("Category");
				$parents = $Category->where("parent=0")->findAll();
				$this->assign("parents", $parents);
				
				$this->assign('vo',$vo);
				$this->display();
			}else{
				exit('编辑项不存在！');
			}
		}else{
			exit('编辑项不存在！');
		}
	}

}